home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue29 / afpelog / AFPELOG.ZIP / afpEventLog.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-10-08  |  6.8 KB  |  226 lines

  1. (*******************************************************************************
  2.  
  3. TafpEventLog Version 1.0
  4. Written by Alfred Petri
  5. Copyright (c) 1997 by Alfred Petri. All rights reserved.
  6.  
  7. Please send comments to alfred_petri@compuserve.com
  8.  
  9.  ******************************************************************************
  10.  *   Permission to use, copy,  modify, and distribute this software and its   *
  11.  *        documentation without fee for any purpose is hereby granted,        *
  12.  *   provided that the above copyright notice appears on all copies and that  *
  13.  *     both the copyright notice and this permission notice appear in all     *
  14.  *                         supporting documentation.                          *
  15.  *                                                                            *
  16.  * NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY *
  17.  *    PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.    *
  18.  *        ALFRED PETRI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY        *
  19.  *                          THE USE OF THIS SOFTWARE.                         *
  20.  ******************************************************************************
  21.  
  22.  
  23. This is a non-visual VCL component that encapsulates the NT 4.x REPORTEVENT
  24. function. The purpose of this component is to facilitate the generation of log
  25. entries in the Windows NT Event-Log.
  26.  
  27. Properties:
  28. - ApplicationName:     Applicationname to appear in Eventlog
  29. - RegisterApplication: If True, a Key of ApplicationName is created in
  30.                                      HKEY_LOCAL_MACHINE
  31.                        \SYSTEM\CurrentControlSet\Services\EventLog\Application
  32.                        If the application is not registered (and thus a
  33.                        corresponding key is not found), EventViewer will
  34.                        not be able to filter messages for this application.
  35. - IncludeUserName:     If True, includes the current user name in the message
  36.                        written to the event log.
  37. - EventType:           Determines the icon to display in Event Viewer.
  38. - EventID:             Integer Positive Number - written to log as is.
  39. - EventCategory:       Integer Positive Number - written to log as is.
  40.  
  41. Methods:
  42. - LogEvent:           Used to write a message to the Event Log. Typical call:
  43.   afpEventLog1.LogEvent('Password Expired!'#13#10'Contact support!');
  44.  
  45.   Notes:
  46. *******************************************************************************)
  47.  
  48. unit afpEventLog;
  49.  
  50. interface
  51.  
  52. uses
  53.   Windows, Registry, Messages, SysUtils, Classes, Graphics, Controls, Dialogs;
  54.  
  55. type
  56.     TEventType = (etError,etWarning,etInformation,etAuditSuccess,etAuditFailure);
  57.  
  58.   TafpEventLog = class(TComponent)
  59.  
  60.   private
  61.     { Private declarations }
  62.     FApplicationName: String;
  63.     FRegisterApplication: Boolean;
  64.     FIncludeUserName: Boolean;
  65.     FEventType: TEventType;
  66.     FEventID: DWord;
  67.     FEventCategory: Word;
  68.     FUserName: String;
  69.  
  70. //    procedure SetupCaption;
  71.  
  72.   protected
  73.     { Protected declarations }
  74.         procedure SetApplicationName(Value: String);
  75.         procedure SetRegisterApplication(Value: Boolean);
  76.         procedure SetIncludeUserName(Value: Boolean);
  77.         procedure SetEventID(Value: DWord);
  78.         procedure SetEventCategory(Value: Word);
  79.  
  80.   public
  81.     { Public declarations }
  82.         constructor Create(AOwner: TComponent); override;
  83.         destructor Destroy; override;
  84.         procedure LogEvent(const Line: String);
  85.  
  86.   published
  87.     { Published declarations }
  88.         property ApplicationName: String
  89.           read FApplicationName write SetApplicationName;
  90.         property RegisterApplication: Boolean
  91.           read FRegisterApplication write SetRegisterApplication;
  92.         property IncludeUserName: Boolean
  93.           read FIncludeUserName write SetIncludeUserName;
  94.         property EventType: TEventType
  95.           read FEventType write FEventType default etInformation;
  96.         property EventID: DWord
  97.           read FEventID write SetEventID;
  98.         property EventCategory: Word
  99.           read FEventCategory write SetEventCategory;
  100.   end;
  101.  
  102. procedure Register;
  103.  
  104. {$R afpEventLog.Res}
  105.  
  106.  
  107. implementation
  108.  
  109. procedure Register;
  110. begin
  111.   RegisterComponents('AFP', [TafpEventLog]);
  112. end;
  113.  
  114.  
  115. constructor TafpEventLog.Create(AOwner: TComponent);
  116. begin
  117.         inherited Create(AOwner);
  118.         ApplicationName     := '!MyApp';
  119.      EventCategory       := 0;
  120.      EventID             := 0;
  121.      EventType           := etInformation;
  122.      IncludeUserName     := True;
  123.      RegisterApplication := True;
  124. end;
  125.  
  126. destructor TafpEventLog.Destroy;
  127. begin
  128.     inherited Destroy;
  129. end;
  130.  
  131. procedure TafpEventLog.SetApplicationName(Value: String);
  132. begin
  133.     if FApplicationName = Value then exit;
  134.     FApplicationName := Value;
  135. end;
  136.  
  137. procedure TafpEventLog.SetRegisterApplication(Value: Boolean);
  138. begin
  139.     if FRegisterApplication = Value then exit;
  140.     FRegisterApplication := Value;
  141. end;
  142.  
  143. procedure TafpEventLog.SetIncludeUserName(Value: Boolean);
  144. begin
  145.     if FIncludeUserName = Value then exit;
  146.     FIncludeUserName := Value;
  147. end;
  148.  
  149. procedure TafpEventLog.SetEventId(Value: DWord);
  150. begin
  151.     if FEventID = Value then exit;
  152.     FEventID := Abs(Value);
  153. end;
  154.  
  155. procedure TafpEventLog.SetEventCategory(Value: Word);
  156. begin
  157.     if FEventCategory = Value then exit;
  158.     FEventCategory := Abs(Value);
  159. end;
  160.  
  161. procedure TafpEventLog.LogEvent(const Line: String);
  162. const
  163.     cRegPath = '\SYSTEM\CurrentControlSet\Services\EventLog\Application';
  164. var
  165.     LogHandle:    THandle;
  166.     OK,OK2Run: Boolean;
  167.     eType: Word;
  168.   eMsg, aName: PChar;
  169.     Reg: TRegistry;
  170.   nSize: Dword;
  171.   VersionInfo : TOSVersionInfo;
  172. begin
  173.     VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
  174.      Ok2Run := False;
  175.     if Windows.GetVersionEx( VersionInfo ) then
  176.         if VersionInfo.dwPlatformId >= VER_PLATFORM_WIN32_NT then
  177.            Ok2Run := True;
  178.   if Ok2Run then
  179.   begin
  180.         if RegisterApplication then
  181.         begin
  182.             Reg := TRegistry.Create;
  183.             try
  184.                 with Reg do
  185.                 begin
  186.                     RootKey := HKEY_LOCAL_MACHINE;
  187.                   OpenKey( cRegPath + '\' + ApplicationName, True );
  188.                     CloseKey;
  189.                   end;
  190.             finally
  191.                 Reg.Free;
  192.             end;
  193.         end;
  194.  
  195.         LogHandle:= OpenEventLog( NIL, PChar(ApplicationName) );
  196.         if Loghandle<>0 then
  197.         begin
  198.           eType := 0;
  199.               case EventType of
  200.                 etError:        eType := 1;
  201.             etWarning:      eType := 2;
  202.                 etInformation:  eType := 4;
  203.                 etAuditSuccess: eType := 8;
  204.                 etAuditFailure: eType := 16;
  205.             end;
  206.  
  207.         FUsername := #13#10;
  208.             If IncludeUserName then
  209.             begin
  210.               nSize := 20 ; // Max UserName
  211.               aName := stralloc ( nSize+1 );
  212.               OK := GetUserName( aName, nSize );
  213.                if not OK then strcopy( aName, 'N/A' );
  214.               FUserName := FUserName + 'User: ' + aName + #13#10;
  215.               strDispose( aName );
  216.             end;
  217.             eMsg := Pchar(FUserName + Line);
  218.  
  219.             ReportEvent(LogHandle, eType, EventCategory, EventID, NIL, 1, 0, @eMsg, NIL);
  220.              CloseEventLog(LogHandle);
  221.         end;
  222.     end;
  223. end;
  224.  
  225. end.
  226.